<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;
/**
 * @property int $id 
 * @property string $oaidMD5 
 * @property string $imeiMD5 
 * @property string $callback
 * @property int $channel
 * @property int $campaignid 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 */
class OcpcKshou extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'ocpc_kshou';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'campaignid' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    /**
     * @param string $oaid_md5
     * @return bool
     */
    public static function existOaidMd5(string $oaid_md5): bool
    {
        if (empty($oaid_md5)) {
            return false;
        }

        return self::where([
            'oaidMD5' => $oaid_md5,
        ])->exists();
    }

    /**
     * @param string $imei_md5
     * @return bool
     */
    public function existImeiMd5(string $imei_md5): bool
    {
        if (empty($imei_md5)) {
            return false;
        }

        return self::where([
            'imeiMD5' => $imei_md5,
        ])->exists();
    }

    /**
     * @param array $params
     * @return int
     */
    public static function addLog(array $params = []): int
    {
        $data = [
            'oaidMD5'      => $params['oaidMD5'] ?? '',
            'imeiMD5'        => $params['imeiMD5'] ?? '',
            'callback'        => $params['callback'] ?? '',
            'channel'   => $params['channel'] ?? '',
            'campaignid'     => $params['campaignid'] ?? '',
            'created_at'  => date('Y-m-d H:i:s'),
            'updated_at'  => date('Y-m-d H:i:s')
        ];

        return self::insertGetId($data);
    }



    /**
     * 处理查询
     * @param $info
     * @return array
     */
    public static function handleSearch($info): array
    {
        log_info(__CLASS__.':'.json_encode($info));
        //oaid 查询
        if (isset($info['oaid']) && $info['oaid']) {
            $res = self::where('oaidMD5', strtolower(md5($info['oaid'])))
//                ->where("channel_id", $info['channel'])
                ->orderBy('created_at', "desc")->first(['id', 'imeiMD5','oaidMD5','callback','channel']);

            if ($res) {
                return $res->toArray();
            }
        }

        if (isset($info['imei']) && $info['imei']) {
            $res = self::where('imeiMD5', strtolower(md5($info['imei'])))
//                ->where("channel_id", $info['channel'])
                ->orderBy('created_at', "desc")->first(['id', 'imeiMD5','oaidMD5','callback','channel']);

            if ($res) {
                return $res->toArray();
            }
        }

        return [];
    }
}